package com.democode.user.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demoCode.common.common.ErrorCode;
import com.demoCode.common.exception.BusinessException;
import com.demoCode.model.auth.dto.AuthLoginDto;
import com.demoCode.model.auth.dto.AuthRegisterDto;
import com.demoCode.model.auth.vo.AuthLoginVo;
import com.demoCode.model.user.dto.PageUserDto;
import com.demoCode.model.user.dto.UpdateUserDto;
import com.demoCode.model.user.entity.User;
import com.demoCode.model.user.vo.UserVo;
import com.democode.user.mapper.UserMapper;
import com.democode.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param authLoginDto
     * @return
     */
    @Override
    public AuthLoginVo userLogin(AuthLoginDto authLoginDto) {
        User user = lambdaQuery()
                .eq(authLoginDto.getPhone() != null, User::getPhone , authLoginDto.getPhone())
                .eq(authLoginDto.getPassword() != null, User::getPassword , authLoginDto.getPassword())
                .one();
        if(user == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号或密码错误");
        }
        AuthLoginVo authLoginVo = new AuthLoginVo();
        authLoginVo.setId(user.getId());
        // 调用sa-token登录
        StpUtil.login(authLoginVo.getId());
        // 获取当前登录 token 值
        String tokenValue = StpUtil.getTokenValue();
        authLoginVo.setToken(tokenValue);
        return authLoginVo;
    }

    /**
     * 用户注册
     * @param authRegisterDto
     * @return
     */
    @Override
    public Boolean userRegister(AuthRegisterDto authRegisterDto) {
        Long count = lambdaQuery()
                .eq(authRegisterDto.getPhone() != null, User::getPhone, authRegisterDto.getPhone())
                .count();
        if (count > 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "手机号已存在");
        }
        User newUser = User.builder()
                .phone(authRegisterDto.getPhone())
                .nickname(authRegisterDto.getNickname())
                .password(authRegisterDto.getPassword())
                .build();
        return save(newUser);
    }

    /**
     * 更新用户信息
     * @param updateUserDto
     * @return
     */
    @Override
    public Boolean updateUser(UpdateUserDto updateUserDto) {
        if (updateUserDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数不能为空");
        }
        if(updateUserDto.getId() == null){
            User newUser = User.builder()
                    .phone(updateUserDto.getPhone())
                    .nickname(updateUserDto.getNickname())
                    .password(updateUserDto.getPassword())
                    .address(updateUserDto.getAddress())
                    .sex(updateUserDto.getSex())
                    .age(updateUserDto.getAge())
                    .build();
            return save(newUser);
        }
        User user = lambdaQuery()
                .eq(updateUserDto.getId() != null, User::getId, updateUserDto.getId())
                .one();
        BeanUtil.copyProperties(updateUserDto,user);
        return updateById(user);
    }

    /**
     * 删除用户
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean removeUser(List<String> ids) {
        if (ids == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        }
        return removeByIds(ids);
    }

    @Override
    public UserVo getUserById(Long id) {
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数不能为空");
        }
        return getUserById(id);
    }


    @Override
    public Boolean userLogout() {
        StpUtil.logout();
        return true;
    }

    /**
     * 分页查询用户列表
     * @param pageUserDto
     * @return 用户列表
     */
    @Override
    public IPage<UserVo> pageUser(PageUserDto pageUserDto) {
        Page<User> page = lambdaQuery()
                .like(StringUtils.isNotBlank(pageUserDto.getUsername()),User::getUsername, pageUserDto.getUsername())
                .like(StringUtils.isNotBlank(pageUserDto.getNickname()),User::getNickname, pageUserDto.getNickname())
                .eq(pageUserDto.getPhone() != null,User::getPhone, pageUserDto.getPhone())
                .eq(pageUserDto.getSex() != null,User::getSex, pageUserDto.getSex())
                .eq(pageUserDto.getAge() != null,User::getAge, pageUserDto.getAge())
                .eq(pageUserDto.getAddress() != null,User::getAddress, pageUserDto.getAddress())
                .eq(pageUserDto.getId() != null,User::getId, pageUserDto.getId())
                .page(new Page<>(pageUserDto.getCurrent(), pageUserDto.getPageSize()));
        //批量转换
        List<UserVo> userVoList = BeanUtil.copyToList(page.getRecords(), UserVo.class);
        IPage<UserVo> pageList = new Page<>();
        //复制属性
        BeanUtils.copyProperties(page, pageList);
        pageList.setRecords(userVoList);
        return pageList;
    }



}




